import s_get from '@dfeidao/atom-web/storage/get';

import get from '@dfeidao/atom-web/local/get';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';

export default async function a002(fd: IFeidaoAiBrowserComponent) {
	//   保存

	const f = fd.data.node.querySelector<HTMLFormElement>('#allot_conditions_form');

	const productid = await s_get('productid');
	// const productid = get(fd, 'productid');
	const connector = f.connector.value;
	const operate = f.operate.value;
	const field_value = f.field_value.value;
	const selectedOptions = f.field.selectedOptions[0].getAttribute('v');
	const obj = JSON.parse(selectedOptions);
	const field = obj.field;
	const fieldtype = obj.fieldtype;
	const role_type_id = get(fd, 'role_type_id');
	const group_id = get(fd, 'group_id');
	const role_allot_id = get(fd, 'role_allot_id');
	const id = get(fd, 'x_id');
	if (!field) {
		error('字段名称不能为空');
		return;
	}
	if (!field_value) {
		error('字段值不能为空');
		return;
	}
	const zj = get(fd, 'zj');

	const query = { productid, role_allot_id, group_id, role_type_id, connector, operate, field, field_value, fieldtype };
	// console.log(query)
	show_loading();
	const res = await nodejs('roler/zj-000007/s002', query);
	hide_loading();
	if (res === 'success') {
		// fire('zj-000004', 'ec-004', group_id, role_allot_id, id);
		if (zj === 'zj-000011') {
			fire('zj-000011', 'ec-004', group_id, role_allot_id, id);
		} if (zj === 'zj-000004') {
			fire('zj-000004', 'ec-004', group_id, role_allot_id, id);
		}
		fire('zj-000007', 'eu-001');
		success('保存成功');


	} else {
		error('保存失败');
	}
}
